package cn.befory.service.impl;

import cn.befory.service.UrlService;
import cn.befory.service.model.GenerateUrlParam;
import cn.befory.support.ShortUrlGenerator;
import cn.hutool.cache.Cache;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


@Service
@Log4j2
public class UrlServiceImpl implements UrlService {

    @Autowired
    @Qualifier("cache")
    private Cache<String,String> cache;

    @Autowired
    private ShortUrlGenerator shortUrlGenerator;

    @Override
    public String generateShortUrl(GenerateUrlParam generateUrlParam) {
        String shortUrl = shortUrlGenerator.generate();
        cache.put(shortUrl,generateUrlParam.getLongUrl());
        return shortUrl;
    }

    @Override
    public String getLongUrl(String shortUrl) {
        return cache.get(shortUrl);
    }

}
